USE [NANS21V]
GO

/****** オブジェクト:  StoredProcedure [dbo].[SigVal_smh]    スクリプト日付: 08/01/2018 09:27:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SigVal_smh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SigVal_smh]
GO

CREATE PROCEDURE [dbo].[SigVal_smh]
	@TAIKAINO int,
	@KYOGINO int,
	@KUMINO int,
	@ROUNDNO int,
	@SUBNO int,
	@SYUMOKUKUBUN int,
	@EXE2FLG int
AS
SELECT *
FROM
(	SELECT DISTINCT 
		RS.KIROKU, RS.SIGINO,
		CASE WHEN RS.KIROKU = 0 THEN '' ELSE dbo.Kiroku(RS.KIROKU, @SYUMOKUKUBUN, 3, 3)END AS SIGIVAL
	FROM TBL_RESULT_SIGI AS RS,
		TBL_KUMI AS TKU, 
		TBL_RESULT AS RE 
	WHERE RS.TAIKAINO = @TAIKAINO 
		AND RS.KYOGINO = @KYOGINO 
		AND RS.ROUNDNO = @ROUNDNO 
		AND RS.SUBNO = @SUBNO 
		AND RS.TAIKAINO = @TAIKAINO
		AND RS.KYOGINO = RE.KYOGINO 
		AND RS.SUBNO = RE.SUBNO 
		AND RS.ROUNDNO = RE.ROUNDNO 
		AND RS.KUMINO = @KUMINO
		AND RS.KUMINO = RE.KUMINO 
		AND RS.LANE = RE.LANE
		AND TKU.TAIKAINO = @TAIKAINO 
		AND TKU.KYOGINO = @KYOGINO 
		AND TKU.SUBNO = @SUBNO 
		AND TKU.ROUNDNO = @ROUNDNO
		AND RS.EXTEND2 LIKE CASE WHEN @EXE2FLG = 0 THEN '' ELSE CONVERT(varchar, @EXE2FLG) END
)AS RS
	ORDER BY CASE WHEN @EXE2FLG = 0 THEN RS.KIROKU  ELSE RS.SIGINO END
RETURN 0
